package com.john.test.fictiongetter;

import org.apache.commons.lang3.StringUtils;

import java.nio.charset.Charset;
import java.util.regex.Pattern;

/**
 * 小说获取页面配置信息
 * @author jiangguangtao on 2016/9/20.
 */
public class FictionGetterConfig {
    // 查询开始URL
    private String beginUrl;
    // 查询结束URL 到此地址就不再查询了
    private String endUrl;


    // 段落拆分表达式 获取的页面内容数据以什么来分段落
    private String paragraphSep = "\\s+";

    // 是否使用页面标题作为小说页的标题
    private String titleType = TitleTypeEnum.NONE.getValue();
    /**
     * 标题内容路径选择符
     * 如果不使用网页标题时使用的选择符，如果usePageTitle=TRUE，则不使用此属性
     */
    private String titleSelector;
    // 获取的标题数据要移除的前缀内容
    private String titlePreRemove;
    // 获取的标题数据要移除的后缀内容
    private String titlePostRemove;

    /**
     * 小说主体内容选择符
     * #contents
     */
    private String contentSelector;

    /**
     * 下一页路径选择符 相对于文档根路径
     * ".readbutton>.readdown a"
     */
    private String nextPageSelector;
    /**
     * 匹配内容中的第一行中的标题行正则
     */
    private Pattern titleMatcherPattern = Pattern.compile("第.{1,10}章", Pattern.CASE_INSENSITIVE);
    /**
     * 编码
     */
    private Charset charset;


    private FictionGetterConfig() {

    }

    /**
     * 创建一个对象
     *
     * @return
     */
    public static FictionGetterConfig begin() {
        return new FictionGetterConfig();
    }

    /**
     * 设置开始地址
     *
     * @param beginUrl
     * @return
     */
    public FictionGetterConfig beginUrl(String beginUrl) {
        checkParamEmpty(beginUrl, "开始地址不能为空");
        this.beginUrl = beginUrl;
        return this;
    }

    /**
     * 设置页面的结束链接
     * <p>不设置也可以，如果找不到下一页链接对象也是认为结束的; 如果未找到文档正文也是认为结束了</p>
     *
     * @param endUrl
     * @return
     */
    public FictionGetterConfig endUrl(String endUrl) {
        this.endUrl = endUrl;
        return this;
    }

    public FictionGetterConfig titlePreRemove(String titlePreRemove) {
        this.titlePreRemove = titlePreRemove;
        return this;
    }

    public FictionGetterConfig titlePostRemove(String titlePostRemove) {
        this.titlePostRemove = titlePostRemove;
        return this;
    }

    /**
     * 设置标题类型
     *
     * @param typeEnum
     * @return
     */
    public FictionGetterConfig titleType(TitleTypeEnum typeEnum) {
        this.titleType = typeEnum.getValue();
        return this;
    }

    /**
     * 设置标题类型
     *
     * @param titleType
     * @return
     */
    public FictionGetterConfig titleType(String titleType) {
        this.titleType = titleType;
        return this;
    }

    /**
     * 如果不使用网页的标题作为小说文档的标题，那么设置小说标题获取路径表达式字符串
     *
     * @param titleSelector
     * @return
     */
    public FictionGetterConfig titleSelector(String titleSelector) {
        this.titleSelector = titleSelector;
        return this;
    }

    /**
     * 小说主体内容选择路径表达式
     *
     * @param contentSelector
     * @return
     */
    public FictionGetterConfig contentSelector(String contentSelector) {
        checkParamEmpty(contentSelector, "小说主体内容路径表达式不能为空！");
        this.contentSelector = contentSelector;
        return this;
    }

    /**
     * 设置获取到的小说主体内容文本的分隔字符表达式
     *
     * @param paragraphSep
     * @return
     */
    public FictionGetterConfig paragraphSep(String paragraphSep) {
        this.paragraphSep = paragraphSep;
        return this;
    }

    /**
     * 设置下一页链接获取路径表达式
     *
     * @param nextPageSelector
     * @return
     */
    public FictionGetterConfig nextPageSelector(String nextPageSelector) {
        checkParamEmpty(nextPageSelector, "下一页链接路径表达式不能为空！");
        this.nextPageSelector = nextPageSelector;
        return this;
    }

    /**
     * 匹配内容中的第一行中的标题行正则
     * @param regex
     * @return
     */
    public FictionGetterConfig titleMatcherPattern(String regex) {
        checkParamEmpty(regex, "标题行匹配正则不能为空！");
        this.titleMatcherPattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        return this;
    }

    public FictionGetterConfig charset(Charset charset) {
        if(null != charset) {
            this.charset = charset;
        }
        return this;
    }


    /**
     * @param str
     * @param msg
     */
    private void checkParamEmpty(String str, String msg) {
        if (StringUtils.isBlank(str)) {
            throw new IllegalArgumentException(msg);
        }
    }

    /**
     * Getter for property 'beginUrl'.
     *
     * @return Value for property 'beginUrl'.
     */
    public String getBeginUrl() {
        return beginUrl;
    }

    /**
     * Getter for property 'endUrl'.
     *
     * @return Value for property 'endUrl'.
     */
    public String getEndUrl() {
        return endUrl;
    }

    /**
     * Getter for property 'titlePreRemove'.
     *
     * @return Value for property 'titlePreRemove'.
     */
    public String getTitlePreRemove() {
        return titlePreRemove;
    }

    /**
     * Getter for property 'titlePostRemove'.
     *
     * @return Value for property 'titlePostRemove'.
     */
    public String getTitlePostRemove() {
        return titlePostRemove;
    }

    /**
     * Getter for property 'paragraphSep'.
     *
     * @return Value for property 'paragraphSep'.
     */
    public String getParagraphSep() {
        return paragraphSep;
    }

    /**
     * Getter for property 'titleType'.
     *
     * @return Value for property 'titleType'.
     */
    public String getTitleType() {
        return titleType;
    }

    /**
     * Getter for property 'titleSelector'.
     *
     * @return Value for property 'titleSelector'.
     */
    public String getTitleSelector() {
        return titleSelector;
    }

    /**
     * Getter for property 'contentSelector'.
     *
     * @return Value for property 'contentSelector'.
     */
    public String getContentSelector() {
        return contentSelector;
    }

    /**
     * Getter for property 'nextPageSelector'.
     *
     * @return Value for property 'nextPageSelector'.
     */
    public String getNextPageSelector() {
        return nextPageSelector;
    }

    public Pattern getTitleMatcherPattern() {
        return titleMatcherPattern;
    }

    /**
     * Getter for property 'charset'.
     *
     * @return Value for property 'charset'.
     */
    public Charset getCharset() {
        return charset;
    }
}
